$.ajax({
    type: 'get',
    url: '/comments',
    success: function(response) {
        console.log(response);
        var html = template('commentsTpl', response);
        $('#commentsBox').html(html);
        var pageHtml = template('pageTpl', response);
        $('#pageBox').html(pageHtml);
    }
});
function changePage(page) {
    console.log(page);
    $.ajax({
        type: 'get',
        url: '/comments',
        data: {
            page: page
        },
        success: function(response) {
            console.log(response);
            var html = template('commentsTpl', response);
            $('#commentsBox').html(html);
            var pageHtml = template('pageTpl', response);
            $('#pageBox').html(pageHtml);
        }
    });
}

$('#commentsBox').on('click', '.state', function() {
    var id = $(this).attr('data-id');
    var state = $(this).attr('data-state');
    state = state == 1 ? 0 : 1;
    $.ajax({
        type: 'put',
        url: `/comments/${id}`,
        data: {
            state: state
        },
        success: function() {
            location.reload();
        }
    });
});

$('#commentsBox').on('click', '.delete', function() {
    if (confirm('确定要删除吗')) {
        var id = $(this).atte('data-id');
        $.ajax({
            type: 'delete',
            url: `/comments/${id}`,
            success: function() {
                location.reload();
            }
        });
    }
});
